package jt.classic.app.gdzc.consuming;

import org.limp.basework.MachiningException;
import org.limp.basework.SimpleBean;
import org.limp.basework.SimpleBean4Table;
import org.limp.basework.SimpleBeanMachining;
import org.limp.basework.Table;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;

import java.sql.Connection;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


/**
 * 资产领用数据加工
 *
 * @author mido
 *
 */
public class ConsumingMachining implements SimpleBeanMachining {
    public SimpleBean machining(SimpleBean bean, Table table,
        HttpServletRequest request, Connection conn, int type)
        throws MachiningException {
        // TODO Auto-generated method stub

        /*资产领用登记*/
        if (table.getName().equals("APP_GDZC_LY") && (type == 0)) {
            List list = (List) bean.getResource().get("APP_GDZC_LY");

            if ((list != null) && !list.isEmpty()) {
                BaseworkUtil util = new BaseworkUtil();

                for (int i = 0; i < list.size(); i++) {
                    Map temp = (Map) list.get(i);

                    String gzid = (String) temp.get("GZID");

                    SimpleBean4Table sbt = new SimpleBean4TableImpl("APP_GDZC");
                    sbt.getResource().put("GZID", gzid);
                    sbt.getResource().put("GZSTATUS", "1");

                    try {
                        util.manualUpdate1(sbt, conn);
                    } catch (Exception ex) {
                        throw new MachiningException("Consuming machining error.",
                            ex);
                    }
                }
            }
        }
        /*资产领用审核*/
        else if (table.getName().equals("APP_GDZC_LY") && (type == 3)) {
            List list = (List) bean.getResource().get("APP_GDZC_LY");

            if ((list != null) && !list.isEmpty()) {
                BaseworkUtil util = new BaseworkUtil();

                for (int i = 0; i < list.size(); i++) {
                    Map temp = (Map) list.get(i);

                    String gzid = (String) temp.get("GZID");
                    String lystatus = (String) temp.get("LYSTATUS");

                    SimpleBean4Table sbt = new SimpleBean4TableImpl("APP_GDZC");

                    if (lystatus.equals("-1")) {
                        sbt.getResource().put("GZID", gzid);
                        sbt.getResource().put("GZSTATUS", "0");
                    } else {
                        sbt.getResource().put("GZID", gzid);
                        sbt.getResource().put("GZSTATUS", "1");
                    }

                    try {
                        util.manualUpdate1(sbt, conn);
                    } catch (Exception ex) {
                        throw new MachiningException("Consuming machining error.",
                            ex);
                    }
                }
            }
        }

        return null;
    }
}
